#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QDebug>
#include <QLabel>
#include <QLCDNumber>

int MainWindow::pvp_flag=1;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_qipan(new qipan(this)),
    m_stepCount(1),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setFixedSize(800,670);

    m_logic = new Gamelogic;

    int boardSize = 0;
    m_qipan->getBoardInfo(boardSize);
    m_logic->setQipanBoard(m_qipan->getBoardInfo(boardSize),boardSize);
    /* 设置布局 */
    QWidget * centerWidget = new QWidget(this);
    setCentralWidget(centerWidget);
    QVBoxLayout * layout = new QVBoxLayout(centerWidget);
    layout->addWidget(m_qipan);

    m_player = false;
    //m_flag = 1;
    /* 添加开始游戏按钮 */
    QPushButton *startButton = new QPushButton(this);
    startButton->setText("开始游戏");
    startButton->setGeometry(660,60,90,40);
    startButton->show();

    /* 添加重新开始按钮 */
    QPushButton *restartButton = new QPushButton(this);
    restartButton->setText("重新开始");
    restartButton->setGeometry(660,150,90,40);
    restartButton->show();

    /* 添加悔棋按钮 */
    QPushButton *repentButton = new QPushButton(this);
    repentButton->setText("悔棋");
    repentButton->setGeometry(660,240,90,40);
    repentButton->show();

    /* 玩家对战 */
    QPushButton *playerButton = new QPushButton(this);
    playerButton->setText("玩家对战");
    playerButton->setGeometry(660,420,90,40);
    playerButton->show();

    /* 添加倒计时 */
    timer = new QTimer(this);
    QLCDNumber * lcdNumber = new QLCDNumber(this);
    lcdNumber->setDecMode();
    lcdNumber->setDigitCount(8);
    lcdNumber->setGeometry(660,330,100,30);
    lcdNumber->show();
    m_count = 0;
    connect(timer, &QTimer::timeout, [=]()
    {
        m_count += 1;  // 每次定时器触发，计数值加1
        lcdNumber->display(m_count);  // 在QLCDNumber上显示更新后的计数值
    });


    /* 信号和槽 */
    connect(m_qipan,&qipan::chessClick,this,&MainWindow::handleChessClickSlot);
    connect(m_logic,&Gamelogic::Aiposition,this,&MainWindow::handleAiChessPosSlot);
    /* 开始游戏 */
    connect(startButton,&QPushButton::clicked,this,[=]()
    {
        m_qipan->flag = 1;
        timer->start(1000);
    });

    /* 重新开始 */
    connect(restartButton,&QPushButton::clicked,this,[this]()
    {
        m_qipan->initBoard();

        m_qipan->update();
        m_qipan->flag = 1;
        m_map.clear();
        m_stepCount = 1;
    });

    /* 悔棋 */
    connect(repentButton,&QPushButton::clicked,this,[this]()
    {
        if(m_stepCount > 1)
        {
            qDebug() << m_stepCount <<endl;
            m_stepCount--;
            m_map[m_stepCount--].pop_back();
            m_map[m_stepCount].pop_back();

            qDebug() << m_stepCount <<endl;
            m_qipan->initBoard();
             m_qipan->update();
            printBoard();
            m_qipan->update();
        }
    });

    connect(playerButton,&QPushButton::clicked, this,[=]()
    {
        m_player = true;
        startButton->click();

    });

}

MainWindow::~MainWindow()
{
    delete ui;
}




void MainWindow::handleChessClickSlot(int row,int col)
{
    if(m_player == false)
    {
        m_qipan->setBoardInfo(row,col,PLAYER);
        m_qipan->update();
        QVector<int> nums = {row,col,PLAYER};
        m_map[m_stepCount++] = nums;

        if(m_logic->CheckIsCount(row,col,PLAYER,5) == true)
        {
            /* 玩家获胜 */
            QMessageBox::information(this,"游戏结束","玩家获胜");
            m_qipan->flag = 0;
            timer->stop();
        }
        else
        {
            m_logic->computerMove();
        }
    }
    else
    {
        if(pvp_flag == 1)
        {
            m_qipan->setBoardInfo(row,col,PLAYER);
            m_qipan->update();
            QVector<int> nums = {row,col,PLAYER};
            m_map[m_stepCount++] = nums;

            if(m_logic->CheckIsCount(row,col,PLAYER,5) == true)
            {
                /* 玩家获胜 */
                QMessageBox::information(this,"游戏结束","玩家1获胜");
                m_qipan->flag = 0;
                timer->stop();
            }
            pvp_flag = 2;
        }
        else
        {
            qDebug() << pvp_flag << endl;
            m_qipan->setBoardInfo(row,col,COMPUTER);
            m_qipan->update();
            QVector<int> nums = {row,col,COMPUTER};
            m_map[m_stepCount++] = nums;

            if(m_logic->CheckIsCount(row,col,COMPUTER,5) == true)
            {
                /* 玩家获胜 */
                QMessageBox::information(this,"游戏结束","玩家2获胜");
                m_qipan->flag = 0;
                timer->stop();
            }
            pvp_flag = 1;
        }
    }
}

void MainWindow::handleAiChessPosSlot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,COMPUTER);

    m_qipan->update();
    QVector<int> nums = {row,col,COMPUTER};
    m_map[m_stepCount++] = nums;
    if(m_logic->CheckIsCount(row,col,COMPUTER,5))
    {
        QMessageBox::information(this,"游戏结束","AI获胜");
        m_qipan->flag = 0;
        timer->stop();
    }

}

void MainWindow::printBoard()
{
    QVector<int> nums;
    for(int idx = 1;idx < m_stepCount;idx++)
    {
        nums =  m_map[idx];
        for(int j = 0;j < 3;j++)
        {
            m_qipan->setBoardInfo(nums[0],nums[1],(Role)nums[2]);
        }

    }
}

